package com.sk.weichat.ui.message.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.adapter.SearchFriendAdapter;
import com.sk.weichat.adapter.SearchRoomAdapter;
import com.sk.weichat.adapter.SearchUserAdapter;
import com.sk.weichat.bean.AddAttentionResult;
import com.sk.weichat.bean.AttentionUser;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.SearchUserAndRoomBean;
import com.sk.weichat.bean.User;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.bean.message.NewFriendMessage;
import com.sk.weichat.bean.message.XmppMessage;
import com.sk.weichat.broadcast.CardcastUiUpdateUtil;
import com.sk.weichat.broadcast.MsgBroadcast;
import com.sk.weichat.broadcast.MucgroupUpdateUtil;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.db.dao.ChatMessageDao;
import com.sk.weichat.db.dao.FriendDao;
import com.sk.weichat.db.dao.NewFriendDao;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.helper.FriendHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.ui.groupchat.ApplyForAddGroupActivity;
import com.sk.weichat.ui.groupchat.OpenGroupInfoActivity;
import com.sk.weichat.ui.groupchat.SelectContactsActivity;
import com.sk.weichat.ui.me.PersonalCenterActivity;
import com.sk.weichat.ui.message.MucChatActivity;
import com.sk.weichat.ui.nearby.UserListGatherActivity;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.SelectionFrame;
import com.sk.weichat.xmpp.ListenerManager;
import com.sk.weichat.xmpp.listener.ChatMessageListener;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import okhttp3.Call;

/**
 * 网络搜索用户和群
 */
public class SearchUserAndRoomActivity extends EditBaseActivity implements View.OnClickListener {

    private ImageView iv_title_left;
    private EditText et_search_user;
    private ImageView ivClear;
    private TextView tv_finish_search_user;

    private LinearLayout ll_layout_search_empty;
    private LinearLayout linearTip;
    private TextView tvSearchText;


    private LinearLayout ll_query_user;
    private TextView tv_more_query_user;
    private RecyclerView recy_user;

    private LinearLayout ll_query_group;
    private TextView tv_more_query_group;
    private RecyclerView recy_group;


    private String search_user;

    private String mLoginUserId;
    private SearchFriendAdapter mSearchFriendAdapter;
    private List<User> userList = new ArrayList<>();
    private SearchRoomAdapter mSearchRoomAdapter;
    private List<SearchUserAndRoomBean.RoomsBean> roomList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_user_and_room);
        getSupportActionBar().hide();

        search_user = getIntent().getStringExtra("search_user");//搜索内容
        mLoginUserId = coreManager.getSelf().getUserId();
        initView();
        onLoadData(search_user);
    }


    private void initView() {
        iv_title_left = findViewById(R.id.iv_title_left);
        et_search_user = findViewById(R.id.et_search_user);
        ivClear = findViewById(R.id.iv_clear_user);
        tv_finish_search_user = findViewById(R.id.tv_finish_search_user);
        ll_layout_search_empty = findViewById(R.id.ll_layout_search_empty);
        linearTip = findViewById(R.id.linear_search_tip);
        tvSearchText = findViewById(R.id.tv_search_text);
        ll_query_user = findViewById(R.id.ll_query_user);
        tv_more_query_user = findViewById(R.id.tv_more_query_user);
        recy_user = findViewById(R.id.recy_user);

        ll_query_group = findViewById(R.id.ll_query_group);
        tv_more_query_group = findViewById(R.id.tv_more_query_group);
        recy_group = findViewById(R.id.recy_group);

        mSearchFriendAdapter = new SearchFriendAdapter(userList);
        recy_user.setLayoutManager(new LinearLayoutManager(this));
        recy_user.setAdapter(mSearchFriendAdapter);
        mSearchFriendAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent;
                switch (view.getId()) {
                    case R.id.layout_search_friend_item://点击item
                        intent = new Intent(SearchUserAndRoomActivity.this, PersonalCenterActivity.class);
                        intent.putExtra(AppConstant.EXTRA_USER_ID, userList.get(position).getUserId());
                        startActivity(intent);
                        break;

                    case R.id.layout_search_attention_btn://关注按钮
                        AttentionUser attentionUser = userList.get(position).getFriends();
                        if (attentionUser != null && (attentionUser.getStatus() == Friend.STATUS_FRIEND || attentionUser.getStatus() == Friend.STATUS_ATTENTION)) {
                            //取消关注
                            showCancelAttentionDialog(userList.get(position));
                        } else {
                            //关注
                            doAddAttention(userList.get(position));
                        }
                        break;
                }
            }
        });

        mSearchRoomAdapter = new SearchRoomAdapter(coreManager, roomList);
        recy_group.setLayoutManager(new LinearLayoutManager(this));
        recy_group.setAdapter(mSearchRoomAdapter);
        mSearchRoomAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.layout_search_group_item:
                        //进入公开群信息页面
                        startActivity(new Intent(SearchUserAndRoomActivity.this, OpenGroupInfoActivity.class)
                                .putExtra("RoomId",roomList.get(position).getId()));

                        break;
                    case R.id.tv_search_add:
                        //加群
                        roomAutoJoin(roomList.get(position));
                        break;
                }
            }
        });
        iv_title_left.setOnClickListener(this);
        tv_finish_search_user.setOnClickListener(this);
        ivClear.setOnClickListener(this);
        tv_more_query_user.setOnClickListener(this);
        tv_more_query_group.setOnClickListener(this);
        linearTip.setOnClickListener(this);
        et_search_user.setText(search_user);
        et_search_user.setSelection(search_user.length());
        et_search_user.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ll_layout_search_empty.setVisibility(View.GONE);
                ll_query_user.setVisibility(View.GONE);
                ll_query_group.setVisibility(View.GONE);
                userList.clear();
                mSearchFriendAdapter.notifyDataSetChanged();
                roomList.clear();
                mSearchRoomAdapter.notifyDataSetChanged();
                if (s == null || s.length() == 0) {
                    ivClear.setVisibility(View.INVISIBLE);
                    linearTip.setVisibility(View.GONE);
                    return;
                }
                ivClear.setVisibility(View.VISIBLE);
                linearTip.setVisibility(View.VISIBLE);
                String tipStr = "网络查找“<font color='#8d76fe'>" + s + "</font>”相关的人和群";
                tvSearchText.setText(Html.fromHtml(tipStr));

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_search_user.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    onLoadData(et_search_user.getText().toString());
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
    private void onLoadData(String keyword) {
        if (TextUtils.isEmpty(search_user)) {
            return;
        }
        DialogHelper.showDefaulteMessageProgressDialog(this);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("keyword", keyword);
        HttpUtils.post().url(coreManager.getConfig().SEARCH_USER_ROOM)
                .params(params)
                .build()
                .execute(new BaseCallback<SearchUserAndRoomBean>(SearchUserAndRoomBean.class) {

                    @Override
                    public void onResponse(ObjectResult<SearchUserAndRoomBean> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1 && result.getData() != null) {
                            if (result.getData().getUsers().size() == 0 && result.getData().getRooms().size() == 0) {
                                ll_layout_search_empty.setVisibility(View.VISIBLE);
                            } else {
                                ll_layout_search_empty.setVisibility(View.GONE);
                            }

                            /*
                             *用户
                             * */
                            if (result.getData().getUsers().size() == 0) {
                                ll_query_user.setVisibility(View.GONE);
                            } else {
                                ll_query_user.setVisibility(View.VISIBLE);
                                if (userList.size() > 0) {
                                    userList.clear();
                                }
                                if (result.getData().getUsers().size() > 3) {
                                    tv_more_query_user.setVisibility(View.VISIBLE);
                                    for (int i = 0; i < 3; i++) {
                                        userList.add(result.getData().getUsers().get(i));
                                    }
                                } else {
                                    tv_more_query_user.setVisibility(View.GONE);
                                    userList.addAll(result.getData().getUsers());
                                }
                                mSearchFriendAdapter.notifyDataSetChanged();
                            }

                            /*群*/
                            if (result.getData().getRooms().size() == 0) {
                                ll_query_group.setVisibility(View.GONE);
                            } else {
                                ll_query_group.setVisibility(View.VISIBLE);
                                if (roomList.size() > 0) {
                                    roomList.clear();
                                }
                                if (result.getData().getRooms().size() > 3) {
                                    tv_more_query_group.setVisibility(View.VISIBLE);
                                    for (int i = 0; i < 3; i++) {
                                        roomList.add(result.getData().getRooms().get(i));
                                    }
                                } else {
                                    tv_more_query_group.setVisibility(View.GONE);
                                    roomList.addAll(result.getData().getRooms());
                                }

                                mSearchRoomAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.dismissProgressDialog();
                        ToastUtil.showToast(SearchUserAndRoomActivity.this, e.getMessage());
                    }
                });
    }

    @Override
    public void onClick(View v) {

        if (v == iv_title_left) {
            finish();
        } else if (v == tv_finish_search_user) {
            EventBus.getDefault().post("cancle");
        } else if (v == linearTip) {
            linearTip.setVisibility(View.GONE);
            onLoadData(et_search_user.getText().toString());
        } else if (v == ivClear) {
            et_search_user.setText("");
        } else if (v == tv_more_query_user) {
            startActivity(new Intent(this, UserListGatherActivity.class).putExtra("search_user", et_search_user.getText().toString()));
        }else if (v==tv_more_query_group){
            startActivity(new Intent(this, NetSearchRoomsActivity.class).putExtra("search_user", et_search_user.getText().toString()));
        }
    }

    private void showCancelAttentionDialog(User user) {

        SelectionFrame mSF = new SelectionFrame(this);
        mSF.setSomething(getString(R.string.dialog_title), getString(R.string.sure_cancel_attention), new SelectionFrame.OnSelectionFrameClickListener() {
            @Override
            public void cancelClick() {
            }

            @Override
            public void confirmClick() {
                cancelAttention(user);
            }
        });
        mSF.show();
    }

    // 点击加好友就调用这个方法，
    private void doAddAttention(User user) {

        if (user.getFriends() == null) {
            AttentionUser attentionUser = new AttentionUser();
            attentionUser.setStatus(Friend.STATUS_UNKNOW);
            user.setFriends(attentionUser);
        }

        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("toUserId", user.getUserId());

        DialogHelper.showDefaulteMessageProgressDialog(SearchUserAndRoomActivity.this);

        // 首先是调接口，
        HttpUtils.get().url(coreManager.getConfig().FRIENDS_ATTENTION_ADD)
                .params(params)
                .build()
                .execute(new BaseCallback<AddAttentionResult>(AddAttentionResult.class) {

                    @Override
                    public void onResponse(ObjectResult<AddAttentionResult> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1 && result.getData() != null) {

                            FriendHelper.updateFriendRelationship(mLoginUserId, user);
                            // Friend friend = FriendDao.getInstance().getFriend(mLoginUserId, user.getUserId());
                            if (result.getData().getType() == 1 || result.getData().getType() == 3) {
                                // 需要验证就发送打招呼的消息，
                                if (user.getFriends() == null
                                        || (user.getFriends().getReplyStatus() != Friend.STATUS_REPLAY_BOTH
                                        && user.getFriends().getIsBeenBlack() != 1
                                        && result.getData().getIsFollowed() == 0)) {// 未回复并且未关注过的发送打招呼消息

                                    doSayHello(user, coreManager.getSelf().getNickName() + "关注了你");
                                }
                                if (user.getFriends().getIsBeenBlack() == 1) {//被拉黑的关注时状态还是被拉黑
                                    FriendDao.getInstance().updateBeBlack(mLoginUserId, user.getUserId(), 1);
                                    FriendDao.getInstance().updateFriendStatus(mLoginUserId, user.getUserId(), Friend.STATUS_ATTENTION);
                                } else {
                                    FriendDao.getInstance().updateBeBlack(mLoginUserId, user.getUserId(), 0);
                                    FriendDao.getInstance().updateFriendStatus(mLoginUserId, user.getUserId(), Friend.STATUS_ATTENTION);
                                }

                                user.getFriends().setStatus(Friend.STATUS_ATTENTION);
                                mSearchFriendAdapter.notifyDataSetChanged();
                                ToastUtil.showToast(SearchUserAndRoomActivity.this, "关注对方成功");

                            } else if (result.getData().getType() == 2 || result.getData().getType() == 4) {// 已经是好友了
                                if (user.getFriends() == null ||
                                        (user.getFriends().getReplyStatus() != Friend.STATUS_REPLAY_BOTH
                                                && user.getFriends().getIsBeenBlack() != 0
                                                && result.getData().getIsFollowed() == 0)) {// 未回复并且未关注过的发送打招呼消息

                                    doSayHello(user, coreManager.getSelf().getNickName() + "关注了你，你们已经是好友，可以互发语音");
                                }
                                FriendDao.getInstance().updateFriendStatus(mLoginUserId, user.getUserId(), Friend.STATUS_FRIEND);
                                user.getFriends().setStatus(Friend.STATUS_FRIEND);
                                mSearchFriendAdapter.notifyDataSetChanged();
                            } else if (result.getData().getType() == 5) {
                                ToastUtil.showToast(SearchUserAndRoomActivity.this, R.string.add_attention_failed);
                            }
                        } else {
                            Toast.makeText(SearchUserAndRoomActivity.this, result.getResultMsg() + "", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.dismissProgressDialog();
                        Toast.makeText(SearchUserAndRoomActivity.this, R.string.tip_hello_failed, Toast.LENGTH_SHORT).show();
                        ToastUtil.showErrorNet(SearchUserAndRoomActivity.this);
                    }
                });
    }

    private void cancelAttention(User user) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("toUserId", user.getUserId());
        DialogHelper.showDefaulteMessageProgressDialog(this);

        HttpUtils.get().url(coreManager.getConfig().CANEL_ATTENTION)
                .params(params)
                .build()
                .execute(new BaseCallback<Void>(Void.class) {

                    @Override
                    public void onResponse(ObjectResult<Void> result) {
                        DialogHelper.dismissProgressDialog();
                        if (result.getResultCode() == 1) {
                            ToastUtil.showToast(SearchUserAndRoomActivity.this, result.getResultMsg());

                            //好友取消关注需要在关系通知提醒
                            if (user.getFriends() != null && user.getFriends().getStatus() == Friend.STATUS_FRIEND) {
                                ChatMessage removeChatMessage = new ChatMessage();
                                removeChatMessage.setContent("你取关了" + user.getNickName() + "，解除好友关系");
                                removeChatMessage.setTimeSend(TimeUtils.sk_time_current_time());
                                FriendDao.getInstance().updateLastChatMessage(mLoginUserId, Friend.ID_NEW_FRIEND_MESSAGE, removeChatMessage);

                                NewFriendMessage message = NewFriendMessage.createWillSendMessage(
                                        coreManager.getSelf(), XmppMessage.TYPE_CANCEL_ATTENTION, null, user);

                                NewFriendDao.getInstance().createOrUpdateNewFriend(message);
                                NewFriendDao.getInstance().changeNewFriendState(message.getUserId(), Friend.STATUS_27);
                                ListenerManager.getInstance().notifyNewFriend(mLoginUserId, message, true);

                                CardcastUiUpdateUtil.broadcastUpdateUi(SearchUserAndRoomActivity.this);
                                user.getFriends().setStatus(Friend.STATUS_BE_ATTENTION);
                                mSearchFriendAdapter.notifyDataSetChanged();
                            } else {

                                if (user.getFriends().getIsBeenBlack() == 1) {//被拉黑的关注时状态还是被拉黑
                                    FriendDao.getInstance().updateBeBlack(mLoginUserId, user.getUserId(), 1);
                                    FriendDao.getInstance().updateFriendStatus(mLoginUserId, user.getUserId(), Friend.STATUS_UNKNOW);
                                } else {
                                    FriendDao.getInstance().updateFriendStatus(mLoginUserId, user.getUserId(), Friend.STATUS_UNKNOW);
                                    FriendDao.getInstance().updateBeBlack(mLoginUserId, user.getUserId(), 0);
                                }

                                user.getFriends().setStatus(Friend.STATUS_UNKNOW);
                                mSearchFriendAdapter.notifyDataSetChanged();
                            }
                        } else {
                            ToastUtil.showToast(SearchUserAndRoomActivity.this, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }


    // 打招呼
    private void doSayHello(User user, String text) {
        ChatMessage sayMessage = new ChatMessage();
        sayMessage.setFromUserId(mLoginUserId);
        sayMessage.setFromUserName(coreManager.getSelf().getNickName());
        sayMessage.setToUserId(user.getUserId());
        sayMessage.setContent(text);
        sayMessage.setType(XmppMessage.TYPE_TEXT); //文本类型
        sayMessage.setMsgType(ChatMessage.TYPE_HELLO);
        sayMessage.setMySend(true);
        sayMessage.setMessageState(ChatMessageListener.MESSAGE_SEND_SUCCESS);
        sayMessage.setPacketId(UUID.randomUUID().toString().replaceAll("-", ""));
        sayMessage.setTimeSend(TimeUtils.sk_time_current_time());

        // 这里最终调用smack的发送消息，

        coreManager.sendChatMessage(user.getUserId(), sayMessage);
        ChatMessageDao.getInstance().saveNewSingleChatMessage(mLoginUserId, user.getUserId(), sayMessage);


    }

    /**
     * @author: czl
     * @description 加群
     * @date: 2019/9/18 9:28
     */
    private void roomAutoJoin(SearchUserAndRoomBean.RoomsBean room){
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("roomId", room.getId());
        DialogHelper.showDefaulteMessageProgressDialog(SearchUserAndRoomActivity.this);

        HttpUtils.post().url(coreManager.getConfig().ROOM_AUTO_JOIN)
                .params(params).build().execute(new BaseCallback<String>(String.class) {
            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode()==1){//加入成功 直接进群
                    //进入群聊页面
//                    Intent intent = new Intent(SearchUserAndRoomActivity.this, MucChatActivity.class);
//                    intent.putExtra(AppConstant.EXTRA_USER_ID, room.getJid());
//                    intent.putExtra(AppConstant.EXTRA_NICK_NAME, room.getName());
//                    intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
//                    startActivity(intent);
                    ToMucChatActivity(room);
                    EventBus.getDefault().post("cancle");//关闭页面
                }else if (result.getResultCode()==1040320){//群主开启了加群验证  跳转到发送加群申请页面
                    startActivity(new Intent(SearchUserAndRoomActivity.this, ApplyForAddGroupActivity.class)
                            .putExtra("RoomId",room.getId()));

                }else if (result.getResultCode()==1040319){//已加入群 直接进入
                    ToastUtil.showToast(SearchUserAndRoomActivity.this,result.getResultMsg());
//                    Intent intent = new Intent(SearchUserAndRoomActivity.this, MucChatActivity.class);
//                    intent.putExtra(AppConstant.EXTRA_USER_ID, room.getJid());
//                    intent.putExtra(AppConstant.EXTRA_NICK_NAME, room.getName());
//                    intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
//                    startActivity(intent);
                    ToMucChatActivity(room);
                    EventBus.getDefault().post("cancle");//关闭页面

                }else {
                    ToastUtil.showToast(SearchUserAndRoomActivity.this,result.getResultMsg());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(SearchUserAndRoomActivity.this);
            }
        });

    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(String cancle) {
        if (cancle.equals("cancle")){
            finish();
        }
    }

    /**
     * @author: czl
     * @description 跳转进入群聊页之前 判断本地是否存在该群
     * @date: 2019/9/27 21:21
     */
    private void ToMucChatActivity(SearchUserAndRoomBean.RoomsBean room){
        Friend mFriend = FriendDao.getInstance().getFriend(mLoginUserId, room.getJid());
        if (mFriend==null){
            Friend mCreateFriend = new Friend();
            mCreateFriend.setOwnerId(mLoginUserId);
            mCreateFriend.setUserId(room.getUserId());
            mCreateFriend.setNickName(room.getNickname());
            mCreateFriend.setDescription(room.getDesc());
            mCreateFriend.setRoomId(room.getId());
//            mCreateFriend.setContent(room.get);
            mCreateFriend.setTimeSend(room.getTalkTime());
            mCreateFriend.setRoomFlag(1);
            mCreateFriend.setStatus(Friend.STATUS_FRIEND);
            mCreateFriend.setGroupStatus(0);
            FriendDao.getInstance().createOrUpdateFriend(mCreateFriend);
            // 调用smack加入群组的方法
            // 被邀请加入群组，lastSeconds == 当前时间 - 被邀请时的时间
//            coreManager.joinMucChat(room.getJid(), 0);
        }
        Intent intent = new Intent(SearchUserAndRoomActivity.this, MucChatActivity.class);
        intent.putExtra(AppConstant.EXTRA_USER_ID, room.getJid());
        intent.putExtra(AppConstant.EXTRA_NICK_NAME, room.getName());
        intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
        startActivity(intent);
    }
}
